package com.sharer.last.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sharer.last.base.enums.BusinessExceptionCode;
import com.sharer.last.base.enums.ExceptionPrefixParamEnum;
import com.sharer.last.base.exception.BusinessException;
import com.sharer.last.base.model.PageVO;
import com.sharer.last.base.support.QueryCondition;
import com.sharer.last.base.utils.ConvertUtil;
import com.sharer.last.mapper.LoginLogMapper;
import com.sharer.last.model.dto.log.LoginLogChangeDTO;
import com.sharer.last.model.dto.log.LoginLogQueryDTO;
import com.sharer.last.model.dto.UserAgentDTO;
import com.sharer.last.model.po.LoginLogPO;
import com.sharer.last.model.vo.log.LoginLogVO;
import com.sharer.last.service.LoginLogService;
import com.sharer.last.utils.BeanUtil;
import com.sharer.last.utils.IpAddressUtils;
import com.sharer.last.utils.UserAgentUtils;
import com.sharer.last.wrapper.LoginLogWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;

/**
 * 登录日志业务层实现
 *
 * @ClassName LoginLogServiceImpl
 * @Author wangjin
 * @Date 2023/5/5 20:45
 * @Description
 * @Version 1.0
 */
@Service
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLogPO> implements LoginLogService {
    @Resource
    LoginLogMapper loginLogMapper;

    @Resource
    LoginLogWrapper loginLogWrapper;

    @Resource
    UserAgentUtils userAgentUtils;

    /**
     * 分页查询
     *
     * @param queryDTO
     * @return
     */
    @Override
    public PageVO<LoginLogVO> selectListByPage(LoginLogQueryDTO queryDTO) {
        LambdaQueryWrapper<LoginLogPO> qw   = this.buildQueryWrapper(queryDTO);
        IPage<LoginLogPO>              page = this.page(QueryCondition.getPage(queryDTO.getPage()), qw);
        return loginLogWrapper.toPageVO(page);
    }

    private LambdaQueryWrapper<LoginLogPO> buildQueryWrapper(LoginLogQueryDTO queryDTO) {
        LambdaQueryWrapper<LoginLogPO> qw = new LambdaQueryWrapper<>();

        String startTime = queryDTO.getStartTime();
        if (startTime != null) {
            qw.gt(LoginLogPO::getCreateTime, startTime);
        }
        String endTime = queryDTO.getEndTime();
        if (endTime != null) {
            qw.lt(LoginLogPO::getCreateTime, endTime);
        }
        return qw;
    }

    /**
     * 删除日志
     *
     * @param id
     */
    @Override
    public void removeLoginLogById(Long id) {
        if (ConvertUtil.toInt(id) < 1) {
            throw new BusinessException(BusinessExceptionCode.ID_IS_NULL, ExceptionPrefixParamEnum.LOGIN_LOG);
        }
        // 查询数据是否存在
        LoginLogPO loginLogPO = this.getById(id);
        if (loginLogPO == null) {
            throw new BusinessException(BusinessExceptionCode.DATA_IS_EMPTY, ExceptionPrefixParamEnum.LOGIN_LOG);
        }
        if (!this.removeById(id)) {
            throw new BusinessException(BusinessExceptionCode.DATA_REMOVE_FAIL, ExceptionPrefixParamEnum.LOGIN_LOG);
        }
    }

    /**
     * 创建登录日志
     *
     * @param dto
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void createLoginLog(LoginLogChangeDTO dto) {
        dto.setId(null);
        LoginLogPO   po           = BeanUtil.copy(dto, LoginLogPO.class);
        String       ipSource     = IpAddressUtils.getCityInfo(dto.getIp());
        UserAgentDTO userAgentDTO = userAgentUtils.parseOsAndBrowser(dto.getUserAgent());
        po.setIpSource(ipSource);
        po.setOs(userAgentDTO.getOs());
        po.setBrowser(userAgentDTO.getBrowser());
        if (!this.save(po)) {
            throw new BusinessException(BusinessExceptionCode.DATA_REMOVE_FAIL, ExceptionPrefixParamEnum.LOGIN_LOG);
        }
    }
}
